package com.siyao.redvip.ui.adapter;

import java.util.List;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.siyao.redvip.R;
import com.siyao.redvip.bean.User;
import com.siyao.redvip.callBack.CommItemClickCallBack;
import com.siyao.redvip.components.view.CircleImageView;
import com.siyao.redvip.image.ImageLoaderOptions;
import com.siyao.redvip.utils.ToastUtils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class VipUserAdapter extends BaseAdapter{
	
	private List<User>  users;
	private LayoutInflater mInflater;// 加载list_item
	private Context context;
	private CommItemClickCallBack commItemClickCallBack;//回调函数
	private boolean show=false; 
	
	public VipUserAdapter(Context context) {
		this.context = context;
		mInflater = LayoutInflater.from(context);
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	@Override
	public int getCount() {
		return users.isEmpty() ? 0 : users.size();
	}

	@Override
	public Object getItem(int position) {
		return position < 0 ? 0 : position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void setCommItemClickCallBack(CommItemClickCallBack commItemClickCallBack) {
		this.commItemClickCallBack = commItemClickCallBack;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if(convertView==null){
			viewHolder = new ViewHolder();
			convertView=mInflater.inflate(R.layout.item_vip_user, null);
			viewHolder.tv_username = (TextView) convertView.findViewById(R.id.tv_username);
			viewHolder.iv_user_icon =  (ImageView) convertView.findViewById(R.id.iv_user_icon);
			convertView.setTag(viewHolder);
		}else{
			viewHolder=(ViewHolder) convertView.getTag();
		}
		User bean = users.get(position);
		viewHolder.tv_username.setText(bean.getGet_name());
		ImageLoader.getInstance().displayImage("", viewHolder.iv_user_icon, ImageLoaderOptions.getHeaderOptions());
		viewHolder.iv_user_icon.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				commItemClickCallBack.click(position);
			}
		});
		return convertView;
	}
	
	private static class ViewHolder {
		public TextView tv_username;
		public ImageView iv_user_icon;
	}
	
	public void setDataChanged(List<User> mDatas) {
        if (mDatas != null && !mDatas.isEmpty()) {
            this.users = mDatas;
            this.notifyDataSetChanged();
        }
    }

    public void clear() {
        if (users != null && !users.isEmpty()) {
            this.users.clear();
        }
        this.notifyDataSetChanged();
    }
    
}
